;  This file is part of tornado.
; 
;    tornado is free software: you can redistribute it And/Or modify
;    it under the terms of the GNU General Public License As published by
;    the Free Software Foundation, either version 3 of the License, Or
;    any later version.
; 
;    tornado is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE.  See the
;    GNU General Public License For more details.
; 
;    You should have received a copy of the GNU General Public License
;    along With tornado.  If Not, see <http://www.gnu.org/licenses/>. 2
; 

#ENDOFLINE = Chr(13)+Chr(10)

Global logFile.i    = #Null 
Global logConsole.b = #False  

; /////////////////////////////////////////////////////////////////////////////
;
; /////////////////////////////////////////////////////////////////////////////
Procedure.b createLogFile(console.b,client.b=#False)
  
  If client = #False 
    logFile    = CreateFile(#PB_Any,"serverLog.txt")
  Else
    logFile    = CreateFile(#PB_Any,"clientLog.txt")
  EndIf 
  
  logConsole = console 
  If logConsole = #True 
    OpenConsole()
  EndIf 
  FileBuffersSize(logFile,0)
  If logFile
    
    WriteStringN(logFile,LSet("",80,"-"))
    WriteStringN(logFile,"LOG FILE")
    WriteStringN(logFile,"DATE : "+FormatDate("%dd/%mm/%yyyy", Date()))
    WriteStringN(logFile,"TIME : "+FormatDate("%hh:%ii:%ss", Date()))
    WriteStringN(logFile,LSet("",80,"-"))
    WriteStringN(logFile,"")
    WriteStringN(logFile,"")
    WriteStringN(logFile,"")
    WriteStringN(logFile,"")
    ProcedureReturn #True 
  EndIf
  ProcedureReturn #False
EndProcedure

; /////////////////////////////////////////////////////////////////////////////
;
; /////////////////////////////////////////////////////////////////////////////
ProcedureDLL writeLog(message.s)
  
  If Len(message)>80
    For i = 80 To Len(message)-1 Step 80
      message = InsertString(message,#ENDOFLINE,i)
    Next
  EndIf
  
  message = ReplaceString(message,#ENDOFLINE,#ENDOFLINE + Space(Len(FormatDate("%hh:%ii:%ss", Date())+"  :  ")))
  
  WriteStringN(logFile,FormatDate("%hh:%ii:%ss", Date())+"  :  "+message)
  
  If logConsole = #True 
    PrintN(FormatDate("%hh:%ii:%ss", Date())+"  :  "+message)
  EndIf 
  
EndProcedure

; /////////////////////////////////////////////////////////////////////////////
;
; /////////////////////////////////////////////////////////////////////////////
Procedure closeLog()
  CloseFile(logFile)
EndProcedure
